package com.leading.spring.controller;

import com.leading.spring.controller.param.DepatmentParam;
import com.leading.spring.modal.Depatment;
import com.leading.spring.service.DepatmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chennan
 * @create 2021-05
 */

@Api("部门操作")
@RestController
@RequestMapping("/depatment")
public class DepatmentController {

    @Autowired
    private DepatmentService depatmentService;

    //    获取一些配置的信息，得到具体的微服务
    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/create")
    @ApiOperation("添加部门")
    public boolean addDepatment(@RequestBody DepatmentParam depatmentParam) {
        Depatment depatmentDTO = new Depatment();
        BeanUtils.copyProperties(depatmentParam, depatmentDTO);
        depatmentService.addDepatment(depatmentDTO);
        return true;
    }

    @GetMapping("/query/{id}")
    @ApiOperation("根据部门id查询数据")
    public Depatment queryById(@PathVariable("id") Long id) {
        Depatment depatment = depatmentService.queryDepatment(id);
        return depatment;
    }

    @GetMapping("/query-all")
    @ApiOperation("查询所有部门数据")
    public List<Depatment> queryAll() {
        return depatmentService.queryAll();
    }

    @PutMapping("/update")
    @ApiOperation("更改部门数据")
    public Integer updateDepatment(@RequestBody DepatmentParam depatmentParam) {
        Depatment depatmentDTO = new Depatment();
        BeanUtils.copyProperties(depatmentParam, depatmentDTO);
        return depatmentService.updateDepatment(depatmentDTO);
    }

    //注册进来的微服务，获取一些消息
    @GetMapping("/eureka-services")
    @ApiOperation("获取注册中心的基本信息")
    public Object discovery() {
        //获取 微服务列表的清单
        //获取微服务的所有清单
        List<String> services = discoveryClient.getServices();
        System.out.println("所有服务的清单：" + services);
        // 得到一个具体的微服务信息 ,
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + " " + instance.getMetadata() + " " + instance.getUri());
        }
        return this.discoveryClient;
    }
}
